package com.aye.notepad.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.aye.notepad.DatabaseModel.Record;
import com.aye.notepad.DatabaseModel.User;
import com.aye.notepad.R;

import org.litepal.crud.DataSupport;

import java.util.Date;
import java.util.List;

public class RecordActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView iv_backHome,iv_delete,iv_save;
    private TextView tv_title,tv_showTime;
    private EditText et_content,et_title;
    private String sendId;

    private static final String TAG = "RecordActivity";
    private int userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        init();
        //根据sendId是否为空判断用户进行的是添加还是修改，并相应调整页面显示内容
        //获取意图
        Intent intent=this.getIntent();
        //如果用户进行的是添加数据，则sendId为空
       sendId=intent.getStringExtra("id");

        if (sendId==null){
            tv_title.setText("添加记录");
            userId= Integer.parseInt(intent.getStringExtra("userId"));
        }else {
            tv_title.setText("修改记录");
            //修改显示时间的控件可见，显示原来的文本信息
            tv_showTime.setVisibility(View.VISIBLE);
            String time=intent.getStringExtra("note_time");
            String content=intent.getStringExtra("content");
            String title=intent.getStringExtra("title");
            tv_showTime.setText(time);
            et_content.setText(content);
            et_title.setText(title);
        }
    }
    /*****************************点击事件**************************************/
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_save:
                //判断用户进行的是添加数据还是修改数据
                if (sendId==null) {
                    saveData();
                }else {
                    updateData();
                }
                break;
            case R.id.iv_delete:
                //清空当前活动界面
                et_content.setText("");
                break;
            case R.id.iv_backHome:
                //关闭当前活动界面
                finish();
                break;
        }
    }

    private void updateData() {
        String content = et_content.getText().toString();
        String title=et_title.getText().toString();
        if (title==null){
            showMsg("标题不能为空");
        }else {
            //数据修改
            Record record=new Record(title,content);
            int i = record.update(Long.parseLong(sendId));
            if (i==1) {
                //如果修改成功，将数据回传的结果码设置为2
                setResult(2);
                showMsg("修改成功");
                finish();
            } else {
                showMsg("修改失败");
            }
        }
    }

    private void saveData() {
        //获取编辑框中输入的内容
        String content = et_content.getText().toString();
        String title=et_title.getText().toString();
        if (title == null) {
            showMsg("标题不能为空！");
        } else {
            //数据添加
            Record record=new Record(title,content);
            User user= DataSupport.find(User.class,userId);
            record.setDate(String.valueOf(new Date()));
            record.setUser(user);
            boolean i = record.save();
            if (i == true) {
                //如果添加成功，将数据回传的结果码设置为2
                setResult(2);
                List<Record> recordLists = user.getRecordLists();
                for (Record record1:recordLists){
                    Log.i(TAG, "onClick: "+record1.toString());
                }
                showMsg("添加成功");
                finish();
            } else {
                showMsg("添加失败");
            }
        }
    }

    /*****************************自定义方法**************************************/
    private void init() {
        //获取控件对象
        iv_backHome=findViewById(R.id.iv_backHome);
        iv_delete=findViewById(R.id.iv_delete);
        iv_save=findViewById(R.id.iv_save);
        tv_showTime=findViewById(R.id.tv_showTime);
        tv_title=findViewById(R.id.tv_title);
        et_content=findViewById(R.id.et_content);
        et_title=findViewById(R.id.et_title);
        //为控件设监听器
        iv_backHome.setOnClickListener(this);
        iv_save.setOnClickListener(this);
        iv_delete.setOnClickListener(this);
    }
    //Toast
    public void showMsg(String s){
        Toast.makeText(RecordActivity.this,s,Toast.LENGTH_SHORT).show();
    }
}
